# -*- coding: utf-8 -*-
"""
Created on Mon May 11 03:22:52 2020

@author: Farman
"""

import joblib
import pathlib
import LandsatFileNameParser as parser


work_dir = pathlib.Path('.')
file_name = 'LandsatDataFiles.list'

#------------------------------------------------------------------------------

with open(work_dir / file_name) as f:
    files = f.readlines()

files = [file.rstrip() for file in files]
        
#------------------------------------------------------------------------------


dict_path_row_year = {} # path, row, year

for file in files:
    file_with_path = file
    file = pathlib.Path(file).name
    cells = parser.parse(file)
    cells = cells[3:]
    path, row, year, month, day, *tail = cells

    dict_row_year = dict_path_row_year.get(path)
    
    if dict_row_year == None:
        dict_row_year = {}
        dict_year = {}
        files = []
    else:
        dict_year = dict_row_year.get(row)
        
        if dict_year == None:
            dict_year = {}
            files = []
        else:
            files = dict_year.get(year)
            
            if files == None:
                files = []

    files.append(file_with_path)
    dict_year.update({year : files})
    dict_row_year.update({row : dict_year})
    dict_path_row_year.update({path : dict_row_year})
    
joblib.dump(dict_path_row_year, 'LandsatFileDictInPathRowYear.joblib')